package com.bss.controller;

import com.bss.pojo.Product;
import com.bss.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author 袁飞
 * @company 中原工学院
 * @create 2020-03-14-19:49
 */
@RestController
@RequestMapping("/bss/product")
public class ProductController {
    @Autowired
    ProductService ProductService;
    @RequestMapping("find")
    public Product getProduct(@RequestParam("id") Integer id){
        return ProductService.findProductById(id);
    }
    @RequestMapping("addOrUpd")
    public void addOrUpd(Product Product){
        System.out.println(Product);
        if ((Product.getProductId()!= null)){
            ProductService.updateProduct(Product);
        }else {
            ProductService.addProduct(Product);
        }
    }
    @RequestMapping("del")
    public void delProduct(@RequestParam("id") Integer id){
        ProductService.deleteProductById(id);
    }
    @RequestMapping("getAll")
    public PageInfo<Product> getAll(Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Product> ProductList = ProductService.findAll();
        PageInfo<Product> ProductPageInfo = new PageInfo<Product>(ProductList);
        return ProductPageInfo;
    }
    @RequestMapping("getAllWithCategory")
    public PageInfo getAllWithCategory(Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Product> ProductList = ProductService.findAllWithCategory();
        PageInfo ProductPageInfo = new PageInfo (ProductList);
        System.out.println(ProductPageInfo);
        return ProductPageInfo;
    }
    @RequestMapping("getProductsByCategoryType")
    public List<Product> getProductsByCategoryType(Integer categoryType){
        return ProductService.findProductsByCategoryType(categoryType);
    }
    @RequestMapping("countPrice")
    public BigDecimal countPrice(Integer id,Integer num){
        if(num == null){
            num=0;
        }
        return ProductService.calculatePrice(id,num);
    }
    @RequestMapping("getAllRepertory")
    public ModelAndView getAllRepertory(Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Product> ProductList = ProductService.findAllWithCategory();
        PageInfo ProductPageInfo = new PageInfo (ProductList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo",ProductPageInfo);
        modelAndView.setViewName("/bssmanager/repertory/repertoryList");
        return modelAndView;
    }
    @RequestMapping("query")
    public PageInfo<Product> query(Integer categoryType,String productName,String MaxproductPrice,String MinproductPrice){
        PageHelper.startPage(1,5);
        BigDecimal max;
        BigDecimal min;
        if (MaxproductPrice!=null && MaxproductPrice != ""){
             max = new BigDecimal(MaxproductPrice);
        }else {
             max = new BigDecimal("99999999");
        }
        if(MinproductPrice!=null&&MinproductPrice != ""){
            min = new BigDecimal(MinproductPrice);
        }else{
            min = new BigDecimal("0");
        }
        List<Product> products =ProductService.findProduct(categoryType,productName,max,min);
        PageInfo productPageInfo = new PageInfo(products);
        return productPageInfo;
    }
    @RequestMapping("queryProductStock")
    public ModelAndView queryProductStock(String productName,String MaxproductPrice,String MinproductPrice){
        PageHelper.startPage(1,5);
        BigDecimal max;
        BigDecimal min;
        if (MaxproductPrice!=null && MaxproductPrice != ""){
            max = new BigDecimal(MaxproductPrice);
        }else {
            max = new BigDecimal("99999999");
        }
        if(MinproductPrice!=null&&MinproductPrice != ""){
            min = new BigDecimal(MinproductPrice);
        }else{
            min = new BigDecimal("0");
        }
        List<Product> products =ProductService.findProductStock(productName,max,min);
        PageInfo productPageInfo = new PageInfo(products);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo",productPageInfo);
        modelAndView.setViewName("/bssmanager/repertory/repertoryList");
        return modelAndView;
    }
}
